package com.sx.micowell.framework.model.smartDevice;

import android.text.TextUtils;

import androidx.databinding.ObservableField;

import com.example.mvvm.base.vm.BaseViewModel;
import com.example.mvvm.until.LogUtils;
import com.example.mvvm.until.ToastUtils;
import com.sx.micowell.framework.arouth.RouteManager;
import com.sx.micowell.framework.binding.BindingCommand;
import com.sx.micowell.framework.binding.impl.IBindingAction;
import com.tuya.smart.android.user.api.ILoginCallback;
import com.tuya.smart.android.user.bean.User;
import com.tuya.smart.home.sdk.TuyaHomeSdk;

/**
 * @Author: YY
 * @Time: 2021/9/15
 * @Describe TODO
 */
public class SmartDeviceLoginModel extends BaseViewModel {

    // 手机号码
    public ObservableField<String> phoneNumber = new ObservableField<>("");

    // 登录密码
    public ObservableField<String> loginPassword = new ObservableField<>("");

    //输入手机号
    public BindingCommand<String> phoneNumberOnTextChangeCommand = new BindingCommand<>(s -> {
        LogUtils.d("phoneNumberOnTextChangeCommand: " + s);
        phoneNumber.set(s);
    });

    //输入登录密码
    public BindingCommand<String> loginPwdOnTextChangeCommand = new BindingCommand<>(s -> {
        LogUtils.d("loginPwdOnTextChangeCommand: " + s);
        loginPassword.set(s);
    });

    //登录按钮的点击事件
    public BindingCommand<IBindingAction> loginOnClickCommand = new BindingCommand<>(() -> {

        if (TextUtils.isEmpty(phoneNumber.get())) {
            ToastUtils.showShort("手机号不能为空");
            return;
        }

        if (TextUtils.isEmpty(loginPassword.get())) {
            ToastUtils.showShort("密码不能为空");
            return;
        }

        // 涂鸦SDK 手机号登录
        TuyaHomeSdk.getUserInstance().loginWithPhonePassword("86", phoneNumber.get(),
                loginPassword.get(), new ILoginCallback() {
                    @Override
                    public void onSuccess(User user) {
                        ToastUtils.showShort("登录成功");
                        getContext().startContainerActivity(
                                RouteManager.Companion.getInstance().getDeviceBindListFragment()
                                        .getClass().getCanonicalName()
                        );
                        getContext().finish();
                    }

                    @Override
                    public void onError(String code, String error) {
                        ToastUtils.showShort("code: " + code + "error:" + error);
                    }
                });
    });

    //注册新用户点击事件
    public BindingCommand<IBindingAction> registerOnClickCommand = new BindingCommand<>(new IBindingAction() {
        @Override
        public void call() {
            LogUtils.d("registerOnClickCommand");
            RouteManager.Companion.getInstance().intentDeviceRegister(1);
        }
    });

    //忘记密码的点击事件
    public BindingCommand<IBindingAction> forgetPwdOnClickCommand = new BindingCommand<>(new IBindingAction() {
        @Override
        public void call() {
            LogUtils.d("forgetPwdOnClickCommand");
            RouteManager.Companion.getInstance().intentDeviceRegister(3);
        }
    });

}
